package face.pay.web.com.statistics.service.impl;

import face.pay.constant.UserType;
import face.pay.dao.IncomeInfoDao;
import face.pay.dao.UserInfoDao;
import face.pay.entity.UserInfo;
import face.pay.util.ResultMessage;
import face.pay.util.TimeUtils;
import face.pay.web.com.statistics.service.IuserStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: cjj
 * @Date: 2019/9/17
 * @Description:
 */
@Service
public class UserStatisticsService implements IuserStatisticsService {

    @Autowired
    private IncomeInfoDao incomeInfoDao;

    @Autowired
    private UserInfoDao userInfoDao;


    /**
     * 统计用户数据
     *
     * @param loginUser
     * @param params
     * @return
     */
    @Override
    public ResultMessage statistics(UserInfo loginUser, Map params) {
        //统计用户的收益
        params.put("userId", loginUser.getId());

        Map<String, Object> map = new HashMap<String, Object>();

        Object startTime =  params.get("startTime");

        if(startTime==null){

           params.put("startTime",TimeUtils.dayFormat(new Date()));
        }

        Map<String, BigDecimal> incomeMap = this.incomeInfoDao.tradeTotalAmoumt(params);
        BigDecimal totalMoney = incomeMap.get("totalMoney");
        Map<String, Object> result = new HashMap<>();
        result.put("totalMoney", totalMoney);

        //统计用户商家个数
        Map<String, Object> record = new HashMap<>();
        record.put("recommend_id", loginUser.getId());
        record.put("type", UserType.ENTERPRISE);
        record.put("deleted",0);
        Integer totalEnterprise = this.userInfoDao.countEnterpriseOrAgency(record);
        result.put("totalEnterprise", totalEnterprise);
        //统计用户代理个数
        record.put("type", UserType.AGENCY);
        Integer totalAgency = this.userInfoDao.countEnterpriseOrAgency(record);
        result.put("totalAgency", totalAgency);
        return ResultMessage.createSuccess(result);
    }
}
